<?php

namespace Wap\Controller;
use Think\Controller;

class AgentController extends HomeController
{
    public function index()
    {
        if(!UID){
            $this->redirect('Agent/login');
        }
        $major=M('major')->field('id,title')->select();
        $this->assign('data',null);
        $this->assign('major',$major);
        $this->display();
    }

    /**
     * 学生信息管理界面
     */
    public function sudentManage(){
        $map['introducer_id'] = UID;
        if(!UID){
            $this->error('请先登录',U('login'));
        }
        $map['type'] = 2;
        $total = NULL;
        $model=M('student_info');
        $page = getpage($model, $map, 15, $total);
        $info = $model->where($map)->field("id,name,ID_number,pay_evidence,introducer,create_time,upload_time")->select();
        $this->assign('info',$info);
        $this->assign('page', $page);
        $this->assign('total', $total);
        $this->assign('page_title','学生信息管理');
        $this->display();
    }

    public function editSudentInfo(){
        if(!UID){
            $this->error('请先登录',U('login'));
        }
        $id=I('get.id');
        $data=M('student_info')->where(array('id'=>$id))->find();
        if($data['pay_evidence']!=''){
            $this->error('不能编辑',U('sudentManage')); 
        }
        $major=M('major')->field('id,title')->select();
        $this->assign('major',$major);
        $this->assign('data',$data);
        $this->display('index');
    }

    public function showStudentInfo(){
        if(!is_login()){
            $this->error('请先登录',U('login'));
        }
        $id=I('get.id');
        $data=M('student_info')->where(array('id'=>$id))->find();
        $this->assign('data',$data);
        $this->display();
    }
    
    /**
     * 保存学生信息
     */
    public function saveSudentInfo(){
        if(!UID){
            $this->error('请先登录',U('login'));
        }
        if(IS_POST){
            $data=I('post.');
            $data['upload_time']=empty($data['pay_evidence'])?'':time();
            $data['type']=2;
            $data['introducer_id']=UID;
            if(empty($data['id'])){ //新增数据
                $data['create_time']=time();
                $res = M('student_info')->add($data);
                if(!$res){
                    $this->error ("添加出错");
                }else{
                    $this->success ('添加成功！', U('sudentManage') );
                }
            } else { //更新数据
                $data['update_time']=time();
                $status = M('student_info')->save($data);
                if(false === $status){
                    $this->error ("更新出错");
                }else{
                    $this->success ('更新成功！', U('sudentManage') );
                }
            }
        }
    }


    /**
     * 登录
     */
    public function login()
    {
        if (IS_POST) {
            $username=I('post.username');
            $password=I('post.password');
            $uid = D('Home/Agent')->login($username, $password);
            if(0 < $uid){ //UC登录成功
                $this->success('登录成功！', U('Agent/index'));
            } else { //登录失败
                if($uid==0){
                    $this->error('用户正在审核！', '', true);
                }else if($uid==-3){
                    $this->error('用户已被禁用！', '', true);
                }else{
                    $this->error('用户名或密码不正确！', '', true);
                }
            }
        } else {
            if (UID) {
                $this->redirect('Agent/index');
            }
            $this->assign('page_title','登录');
            $this->display();
        }
    }


    /**用户有注册
     */
    public function register(){
        //获取登录信息
        if (IS_POST) {
            $data['username']=I('post.username');
            $data['password']=I('post.password');
            $repassword=I('post.repassword');
            $data['nickname']=I('post.nickname');
            $data['mobile']=I('post.mobile');
            $data['department']=I('post.department');
            $re=M('Agent')->where(array('mobile'=>$data['mobile']))->find();
            if(is_array($re)){
                $this->error('手机已注册');
            }
            /* 检测密码 */
            if( $data['password'] != $repassword){
                $this->error('密码和重复密码不一致！');
            }
            $uid    =   D('Home/Agent')->register($data);
            if(0 < $uid){ //注册成功
                $this->success('注册成功！',U('login'));
            } else { //注册失败，显示错误信息
                $this->error($this->showRegError($uid));
            }
        }else{
            $department_info=M('department_info')->field('id,title')->select();
            $this->assign('department_info',$department_info);
            $this->display();
        }
    }




    /**
     * 检验表单字段
     */
    public function checkfield(){
        if(!UID){
            $this->error('请先登录',U('login'));
        }
        $where['ID_number']=I('post.ID_number');
        $where['type']=I('post.type');
        $data=M('student_info')->where($where)->find();
        if(is_array($data)){
            $this->ajaxReturn(['status'=>1,"info"=>'身份证号已存在']);
        }else{
            $this->ajaxReturn(['status'=>0]);
        }
    }



    /**
     * 检测密码格式
     */
    private function checkPassword($password = '')
    {
        //只能是8位数字和字母
        if (preg_match('/^[a-zA-Z\d_]{6,}$/', $password))
            return true;
        else
            return false;
    }

}